#include "uart.h"
#include "definitions.h"

void initUART(void)
{
	//set up UART
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4);
	//tts module uart on PC6, PC7
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3);

	//usb virtual uart
	ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
	ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
	//bluetooth uart
	ROM_GPIOPinConfigure(GPIO_PC4_U4RX);
	ROM_GPIOPinConfigure(GPIO_PC5_U4TX);
	//tts uart
	ROM_GPIOPinConfigure(GPIO_PC6_U3RX);
	ROM_GPIOPinConfigure(GPIO_PC7_U3TX);

	//define ports and pins used for uart
	ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, (PA_0|PA_1));
	ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, (PC_4|PC_5));
	//tts enable uart
	ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, (PC_6|PC_7));

	//enable uart interrupts
	ROM_IntEnable(INT_UART0);
	ROM_IntEnable(INT_UART4);
	//enable tts interrupt for uart
	ROM_IntEnable(INT_UART3);

	//enable input and output interrupts for uart
	ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_TX);
	ROM_UARTIntEnable(UART4_BASE, UART_INT_RX | UART_INT_TX);
	ROM_UARTIntEnable(UART3_BASE, UART_INT_RX | UART_INT_TX);
	//set up uart speeds, virtual usb uses 115200, bluetooth uses 9600 (can change using AT commands)
	ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8|UART_CONFIG_PAR_NONE|UART_CONFIG_STOP_ONE));
	ROM_UARTConfigSetExpClk(UART4_BASE, ROM_SysCtlClockGet(), 9600, (UART_CONFIG_WLEN_8|UART_CONFIG_PAR_NONE|UART_CONFIG_STOP_ONE));
	ROM_UARTConfigSetExpClk(UART3_BASE, ROM_SysCtlClockGet(), 9600, (UART_CONFIG_WLEN_8|UART_CONFIG_PAR_NONE|UART_CONFIG_STOP_ONE));

}

void UARTsend(char* input)
{
	//wait till uart ready
	while(ROM_UARTBusy(UART0_BASE));
	while(*input != '\0')
		ROM_UARTCharPut(UART0_BASE, *input++);
}

void bt_UARTsend(char* input)
{
	//wait till uart ready
	while(ROM_UARTBusy(UART4_BASE));
	while(*input != '\0')
		ROM_UARTCharPut(UART4_BASE, *input++);
}

void bt_UARTputc(char c)
{
	//wait till uart ready
	while(ROM_UARTBusy(UART4_BASE));
	ROM_UARTCharPut(UART4_BASE, c);
}

void tts_UARTputc(char c)
{
	//wait till uart ready
	while(ROM_UARTBusy(UART3_BASE));
	ROM_UARTCharPut(UART3_BASE, c);
}
